﻿using EpubFactory.Common;
using EpubFactory.Zhuishu.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EpubFactory.Zhuishu.UI
{
    public partial class SearchForm : Form
    {
        private QueryBookInfo[] books;        
        public SearchForm()
        {
            InitializeComponent();
        }

        private void txtKeyword_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox eObj = sender as TextBox;

            //上下左右
            if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left)
            {
                if (listFuzzy.SelectedIndex > 0)
                    listFuzzy.SelectedIndex--;
            }
            else if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right)
            {
                if (listFuzzy.SelectedIndex < listFuzzy.Items.Count - 1)
                    listFuzzy.SelectedIndex++;
            }
            //回车
            else if (e.KeyCode == Keys.Enter)
            {
                listFuzzy.Visible = false;
                txtKeyword.Text = listFuzzy.SelectedItem.ToString();
                btnSearch_Click(this, e);
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(txtKeyword.Text))
                {
                    RunAsync(() =>
                    {
                        IList<string> dataSource = LibZhuiShu.AutoComplate(txtKeyword.Text);
                        if (dataSource.Count > 0)
                        {
                            RunInMainthread(() =>
                            {
                                listFuzzy.DataSource = dataSource;
                                listFuzzy.Visible = true;
                            });


                        }
                        else
                        {
                            RunInMainthread(() =>
                                {
                                    listFuzzy.Visible = false;
                                });
                        }
                    });

                }
                else
                {
                    listFuzzy.Visible = false;
                }
            }
            txtKeyword.Select(txtKeyword.Text.Length, 1);
        }

        private void listFuzzy_Click(object sender, EventArgs e)
        {
            ListBox eObj = sender as ListBox;
            eObj.Visible = false;
            txtKeyword.Text = eObj.SelectedItem.ToString();
            txtKeyword.Select(txtKeyword.Text.Length, 1);
            btnSearch_Click(sender, e);
        }

        private void listFuzzy_MouseMove(object sender, MouseEventArgs e)
        {
            ListBox eObj = sender as ListBox;
            eObj.SelectedIndex = eObj.IndexFromPoint(e.Location);
        }

        private void listFuzzy_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            string info = (sender as ListBox).Items[e.Index] as string;
            e.Graphics.DrawString(info, e.Font, new SolidBrush(e.ForeColor), e.Bounds);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            listFuzzy.Visible = false;
            lvResult.FullRowSelect = true;
            RunAsync(() =>
            {
                var results = LibZhuiShu.FuzzySearch(txtKeyword.Text, 0, 100);
                if (results == null || !results.Any())
                {
                    RunInMainthread(() =>
                    {
                        MessageBox.Show("没有找到:" + txtKeyword.Text);
                    });
                    return;
                }
                books = results;
                RunInMainthread(() =>
                    {
                        lvResult.Items.Clear();
                        lvResult.BeginUpdate();
                        foreach (var result in results)
                        {
                            ListViewItem item = new ListViewItem();
                            item.Text = result.title;
                            item.SubItems.Add(result.author);
                            item.SubItems.Add(result.site);
                            item.SubItems.Add(StringUtil.FormatWordCount(result.wordCount));
                            if (!string.IsNullOrWhiteSpace(result.retentionRatio))
                            {
                                item.SubItems.Add(result.retentionRatio + "%");
                            }
                            else
                            {
                                item.SubItems.Add("无");
                            }

                            item.SubItems.Add(result.lastChapter);
                            item.SubItems.Add(result.shortIntro);
                            lvResult.Items.Add(item);
                        }
                        lvResult.EndUpdate();
                    });
            });


        }

        private void RunAsync(Action action)
        {
            ((Action)(delegate()
            {
                if (action != null)
                {
                    action.Invoke();
                }
            })).BeginInvoke(null, null);
        }

        private void RunInMainthread(Action action)
        {
            this.BeginInvoke((Action)(delegate()
            {
                if (action != null)
                {
                    action.Invoke();
                }
            }));
        }

        private void lvResult_DoubleClick(object sender, EventArgs e)
        {
            var indices = lvResult.SelectedIndices;
            if (indices != null && indices.Count > 0)
            {
                var form = new BookDetailForm();
                form.Show(books[indices[0]]);
            }
        }
    }
}
